package com.senven.goods.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.senven.common.entity.Goods;
import com.senven.common.entity.Type;
import com.senven.goods.mapper.GoodsMapper;
import com.senven.goods.service.IGoodsService;
import com.senven.common.vo.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gjh
 * @since 2022-08-31
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {
    @Autowired
    private GoodsMapper goodsMapper;


//    后端页面显示所有信息，进行分页
//    @Override
//    public CommonResult selectall(GoodsVo goodsVo, Integer page, Integer size) {
//        ValueOperations forValue = redisTemplate.opsForValue();
//
//
//        Page<Goods> objectPage = new Page<>(page, size);
//
//        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
//
//        if (StringUtils.hasText(goodsVo.getGoodsname())) {
//            wrapper.like("goodsname",goodsVo.getGoodsname());
//        }
//        Page<Goods> goodsPage = goodsMapper.selectPage(objectPage, wrapper);
//        return new CommonResult(2000,"查询角色成功",goodsPage);
//    }



    //后端页面显示所有的菜品信息分页
    @Override
    public CommonResult selectall(String id, Integer currentPage, Integer pageSize, Goods goodsVo) {
        Integer role = goodsMapper.selectRole(id);
        if (role ==1){
            PageHelper.startPage(currentPage,pageSize);
            List<Map> goods = goodsMapper.selectall(goodsVo);
            PageInfo pageInfo = new PageInfo(goods);
            return new CommonResult(2000,"查询角色成功",pageInfo);
        }else{
            PageHelper.startPage(currentPage,pageSize);
           List<Map> goods = goodsMapper.selectid(id,goodsVo);
            PageInfo pageInfo = new PageInfo(goods);
            return new CommonResult(2000,"查询角色成功",pageInfo);
        }
    }

//后台进行添加菜品
    @Override
    @Transactional
    public CommonResult shopsAdd(Goods goods, String id) {
        Integer branchid = goodsMapper.branchid(id);
        Integer goodsid = goodsMapper.selectallBYid();
        Integer typeid = goods.getTypeid();
        if (branchid!=null){
            Integer insert = goodsMapper.insertshop(goods);
            Integer add = goodsMapper.add(branchid, goodsid);
            Integer type = goodsMapper.type(branchid, goodsid,typeid);
            System.out.println(typeid);
            if (insert==1&&add==1&&type==1){
                return new CommonResult(2000,"添加成功",null);
            }else{
                return new CommonResult(5000,"添加失败",null);
            }
        }else {
            return new CommonResult(5000,"没有店铺",null);
        }

    }
//后台进行删除菜品
    @Override
    @Transactional
    public CommonResult goodsDel(Integer goodsid, String id) {
        Integer branchid = goodsMapper.branchid(id);
        if (branchid!=null) {
            int deleteById = goodsMapper.deleteById(goodsid);
            Integer delgb = goodsMapper.delgb(branchid, goodsid);
            if (deleteById ==1 &&delgb==1) {
                return new CommonResult(2000, "删除成功", null);
            } else {
                return new CommonResult(500, "删除失败", null);
            }
        }
        return new CommonResult(5000, "删除失败", null);

    }


//后台修改菜品
    @Override
    public CommonResult goodsUpdate(Goods goods, String id) {
        Integer branchid = goodsMapper.branchid(id);
        if (branchid!=null) {
            Integer tid = goodsMapper.selectType(goods, branchid);
            int updateById = goodsMapper.updateGoods(goods);
            int typeid = goodsMapper.updateType(tid, goods);
            if (updateById != 0 && typeid != 0) {
                return new CommonResult(2000, "修改成功", null);
            } else {
                return new CommonResult(500, "修改失败", null);
            }
        }
        return null;
    }

//手机端显示菜品详情页

    @Override
    public CommonResult selectByid(Integer goodsid, Integer branchid, String id) {
        List<Map> goods = goodsMapper.selectByBId(goodsid,branchid,id);
        return new CommonResult(2000,"查询角色成功",goods);
    }
    //手机端根据店铺显示所有菜品

    @Override
    public CommonResult selectbranchid(Integer branchid, String name, Integer typeid, String id) {

        List<Map> goods = goodsMapper.selectbranchid(branchid,name,typeid,id);
        return new CommonResult(2000,"查询角色成功",goods);
    }
    //手机端详情页添加收藏
    @Override
    public CommonResult collect(Integer goodsid, Integer branchid) {
        Integer selectbycollect = goodsMapper.selectbycollect(goodsid, branchid);

        if (selectbycollect == null){
            int collect = goodsMapper.collect(goodsid,branchid);

            if (collect!=0){
                return new CommonResult(2000,"添加成功",null);
            }else{
                return new CommonResult(500,"添加失败",null);
            }
        }else {
            return new CommonResult(2000,"收藏失败",null);
        }
    }
    //手机端删除收藏
    @Override
    public CommonResult nocollect(Integer id) {
        int collect = goodsMapper.nocollect(id);

        if (collect!=0){
            return new CommonResult(2000,"删除成功",null);
        }else{
            return new CommonResult(500,"删除失败",null);
        }
    }
    // 电脑端显示所有的种类信息
    @Override
    public CommonResult selectType() {
        List<Type> type = goodsMapper.findType();
        System.out.println(type);
        return new CommonResult(2000,"查询成功",type);
    }
//电脑端查询所有的管理员id
    @Override
    public CommonResult selectRole(String id) {
        Integer role = goodsMapper.findRole(id);
        return new CommonResult(2000,"查询成功",role);
    }

    @Override
    public CommonResult selectType1(Integer branchid) {
        List<Map> type1 = goodsMapper.selectType1(branchid);
        System.out.println(type1);
        return new CommonResult(2000,"查询成功",type1);
    }


}
